/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.CheckedTextViewBean;

import java.util.List;

public class ActionImageDetailsEditAdapterHolder extends RecyclerView.Adapter {

    private Context mContext;
    private List<String> intList;

    public static final int IMAGE_DETAILS_EDIT = 0;
    public static final int IMAGE_DETAILS_ADD = 1;
    public static final int IMAGE_DETAILS_WARNING = 2;

    public static final int IMAGE_WARNING_INDEX = -1;

    private SparseArray<String> details;
    private ItemClickListener listener;

    private boolean editEnable = true;


    public ActionImageDetailsEditAdapterHolder(Context context, List<String> intList) {
        this.mContext = context;
        this.intList = intList;
        details = new SparseArray<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (position < intList.size())
            return IMAGE_DETAILS_EDIT;
        else if (position == intList.size())
            return IMAGE_DETAILS_ADD;
        else
            return IMAGE_DETAILS_WARNING;
    }

    @Override
    public int getItemCount() {
        return intList.size() + 2;
    }

    public void setOnItemAddClickListener(ItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == IMAGE_DETAILS_ADD) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_image_details_edit_add, parent, false);
            return new ImageDetailsAddHolder(sView);
        } else {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_image_details_edit, parent, false);
            return new ImageDetailHolder(sView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ImageDetailHolder) {
            ImageDetailHolder imageHolder = (ImageDetailHolder) holder;
            if (position < intList.size()) {
                String imageNumber = mContext.getText(R.string.number_point).toString();
                String text = String.format(imageNumber, position + 1);
                imageHolder.numberData.setText(text);
                if(!TextUtils.isEmpty(intList.get(position))){
                    imageHolder.editDetails.setText(intList.get(position));
                }
                imageHolder.editDetails.addTextChangedListener(new EditImageDetailsChanged(position));
            } else {
                String warning = mContext.getText(R.string.warning).toString();
                imageHolder.numberData.setText(warning);
                imageHolder.editDetails.setHint(mContext.getText(R.string.can_chose).toString());
                if(!TextUtils.isEmpty(details.get(IMAGE_WARNING_INDEX))){
                    imageHolder.editDetails.setText(details.get(IMAGE_WARNING_INDEX));
                }
                imageHolder.editDetails.addTextChangedListener(new EditImageDetailsChanged(IMAGE_WARNING_INDEX));
            }
            imageHolder.editDetails.setEnabled(editEnable);
        } else if (holder instanceof ImageDetailsAddHolder) {
            ImageDetailsAddHolder addHolder = (ImageDetailsAddHolder) holder;
        }
    }

    public boolean isEditEnable() {
        return editEnable;
    }

    public void setWarningTxt(String warningTxt){
        details.put(IMAGE_WARNING_INDEX,warningTxt);
    }

    public void setEditEnable(boolean editEnable) {
        this.editEnable = editEnable;
    }

    class EditImageDetailsChanged implements TextWatcher {
        private int position;

        public EditImageDetailsChanged(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            details.put(position, s.toString());
        }
    }

    public class ImageDetailHolder extends RecyclerView.ViewHolder {
        TextView numberData;
        EditText editDetails;

        public ImageDetailHolder(View view) {
            super(view);
            numberData = view.findViewById(R.id.number);
            editDetails = view.findViewById(R.id.edit_content);
        }
    }

    public class ImageDetailsAddHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView imageAdd;

        public ImageDetailsAddHolder(View view) {
            super(view);
            imageAdd = view.findViewById(R.id.add_content_icon);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null)
                listener.onAddImageClick();
        }
    }

    public SparseArray<String> getInputData() {
        return details;
    }


    public interface ItemClickListener {
        void onAddImageClick();
    }

}
